﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using CarlosAg.ExcelXmlWriter;

namespace PPI.Client.Utils
{
    public class ExportToExcelHelper : IDisposable
    {
        private Workbook m_book;
        private Worksheet m_Sheet;
        private FileInfo m_savedFile;
        public FileInfo SavedFile
        {
            get { return m_savedFile; }
        }

        public ExportToExcelHelper()
        {
            m_book = new Workbook();
            m_Sheet = m_book.Worksheets.Add("Sheet");
            
            // Add some styles to the Workbook
            WorksheetStyle style = m_book.Styles.Add("HeaderStyleParent");
            style.Font.FontName = "Tahoma";
            style.Font.Size = 12;
            style.Font.Bold = true;
            style.Alignment.Horizontal = StyleHorizontalAlignment.Center;
            style.Font.Color = "White";
            style.Interior.Color = "Blue";
            style.Interior.Pattern = StyleInteriorPattern.DiagCross;

            // Create the Default Style to use for everyone
            style = m_book.Styles.Add("HeaderStyleChild");
            style.Font.FontName = "Tahoma";
            style.Font.Size = 12;
            style.Font.Bold = true;
            style.Alignment.Horizontal = StyleHorizontalAlignment.Center;
            style.Font.Color = "Black";
            style.Interior.Color = "#cccccc";
            style.Interior.Pattern = StyleInteriorPattern.Solid;



            style = m_book.Styles.Add("CellStyleChild");
            style.Font.FontName = "Tahoma";
            style.Font.Size = 10;
            style.Interior.Color = "#87CEFA";
            style.Interior.Pattern = StyleInteriorPattern.Solid;


            // Create the Default Style to use for everyone
            style = m_book.Styles.Add("Default");
            style.Font.FontName = "Tahoma";
            style.Font.Size = 10;

            m_Sheet.Table.DefaultColumnWidth = 150;
            
        }

        public ExportToExcelHelper(FileInfo savedFile)
        :this()
        {
            m_savedFile = savedFile;
        }

        public void WriteCell(string text, string style)
        {
            WorksheetRow row = m_Sheet.Table.Rows[m_Sheet.Table.Rows.Count - 1];
            row.Cells.Add(text,DataType.String,style);
        }
        public void WriteCell(string text)
        {
            WriteCell(text, "Default");
        }

        public void WriteHeader(string text)
        {
            WriteCell(text, "HeaderStyleParent");
        }

        public void WriteHeaderChild(string text)
        {
            WriteCell(text, "HeaderStyleChild");
        }
        public void WriteCellChild(string text)
        {
            WriteCell(text, "CellStyleChild");
        }

        public void AddRow()
        {
            m_Sheet.Table.Rows.Add();
        }

        public void Close()
        {
            m_book.Save(m_savedFile.FullName);
        }

        public void Dispose()
        {

            
        }
    }
}
